package Motor;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.lcdui.*;
import javax.microedition.rms.*;


public abstract class Carta{

	private int id;
	private int tc;//definir� si la carta en cuestion es un h�roe(0) o una habilidad(1)
	private int tipo;// 0 -> Arquero, 1 -> Mago, 2 -> Guerrero
	private Image imagen=null;
	private Image icono=null;
	
	abstract void almacenar(RecordStore rs);
	abstract Carta cargar(RecordStore rs, int i);
	
	public static byte[] getByteArray(Image img) {  
        byte[] bytes = null;  
        int width = img.getWidth();  
        int height = img.getHeight();  
        int[] pixels = new int[width * height];  
        img.getRGB(pixels, 0, width, 0, 0, width, height);  
  
        try {  
            // convert int[] to byte[]  
            ByteArrayOutputStream bout = new ByteArrayOutputStream();  
            DataOutputStream dout = new DataOutputStream(bout); 
            // then the pixels  
            for (int i = 0; i < pixels.length; i++) {  
                dout.writeInt(pixels[i]);  
            }  
            bytes = bout.toByteArray();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
  
        return bytes;  
    }
	
	public static Image scaleImage(Image original, int newWidth, int newHeight)
    {        
        int[] rawInput = new int[original.getHeight() * original.getWidth()];
        original.getRGB(rawInput, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());
        
        int[] rawOutput = new int[newWidth*newHeight];        

        // YD compensates for the x loop by subtracting the width back out
        int YD = (original.getHeight() / newHeight) * original.getWidth() - original.getWidth(); 
        int YR = original.getHeight() % newHeight;
        int XD = original.getWidth() / newWidth;
        int XR = original.getWidth() % newWidth;        
        int outOffset= 0;
        int inOffset=  0;
        
        for (int y= newHeight, YE= 0; y > 0; y--) {            
            for (int x= newWidth, XE= 0; x > 0; x--) {
                rawOutput[outOffset++]= rawInput[inOffset];
                inOffset+=XD;
                XE+=XR;
                if (XE >= newWidth) {
                    XE-= newWidth;
                    inOffset++;
                }
            }            
            inOffset+= YD;
            YE+= YR;
            if (YE >= newHeight) {
                YE -= newHeight;     
                inOffset+=original.getWidth();
            }
        }               
        return Image.createRGBImage(rawOutput, newWidth, newHeight, false);        
    }
	public Image getImagen() {
		return imagen;
	}
	public void setImagen(Image imagen) {
		this.imagen = imagen;
	}
	public int getTipoCarta() {
		return tc;
	}
	public void setTipoCarta(int tc) {
		this.tc = tc;
	}
	public int getTipo() {
		return tipo;
	}
	public void setTipo(int tipo) {
		this.tipo = tipo;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Image getIcono() {
		return icono;
	}
	public void setIcono(Image icono) {
		this.icono = icono;
	}
	
	public abstract Carta copiar();
	
	public String mostrar() {
		String s=null;
		if(this!=null){
			s=""+this.id;
		}
		return s;
	}
	
}
